Function.RegisterNamespace("System.EnvironmentStrategy");

System.EnvironmentStrategy.Rhino=function(){
    var _newline=null;

    // IEnvironmentStrategy members
    this.Execute=function(command,parameters,voidOutput){
        return runCommand.apply(Object.Global(),[command].concat(parameters))+'';
    };

    this.Exit=function(errorCode){
        quit(errorCode);
    };

    this.GetNewLine=function(){
        if(_newline!=undefined)return _newline;
        return _newline=java.lang.System.getProperty("line.separator")+'';
    };

    this.GetParameters=function(){
        var args={
            named:{},
            unnamed:[]
        };
        var params=Object.Global().arguments;
        for(var i=0;i<params.length;i++){
            var param=String.Trim(params[i]+'');
            if(String.StartsWith(param,'/')&&String.Contains(param,':')){
                param=String.TrimStart(param,'/').split(':');
                args.named[param[0].toLowerCase()]=param.slice(1).join(':');
            }else args.unnamed.push(param);
        }
        return args;
    };
    
    this.GetWorkingDirectory=function(){
        return System.IO.Path.Normalize(java.lang.System.getProperty("user.dir")+'');
    };

    this.Write=function(message1,message2,messageN){
        java.lang.System.out.print(Array.prototype.slice.call(arguments,0).join(''));
    };
    
    this.WriteError=function(message1,message2,messageN){
        java.lang.System.err.print(Array.prototype.slice.call(arguments,0).join(''));
    };

    // IStrategySpecification members
    this.IsSatisfiedBy=function(candidate){
        return (typeof(java)!='undefined' && typeof(environment)!='undefined' && typeof(defineClass)=="function" && typeof(loadClass)=="function");
    };
};

System.EnvironmentStrategy.Rhino.Implement(System.EnvironmentStrategy.IEnvironmentStrategy,'System.EnvironmentStrategy.Rhino');
System.EnvironmentStrategy.Rhino.Implement(System.Script.Strategy.IStrategySpecification,'System.EnvironmentStrategy.Rhino');

System.Environment.Strategies.Add(System.EnvironmentStrategy.Rhino);
